package com.itheima.controller;

import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    // 分页条件查询
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        log.info("班级分页查询请求参数: {}", clazzQueryParam);
        PageResult pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    // 查询班级列表
    @GetMapping("/list")
    public Result list(){
        log.info("查询所有班级:");
        List<Clazz> classList = clazzService.list();
        return Result.success(classList);
    }

    // 根据ID删除班级
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据ID删除班级: {}", id);
        clazzService.deleteById(id);
        return Result.success();
    }

    // 添加班级
    @PostMapping
    public Result save(@RequestBody Clazz clazz){
        log.info("请求参数clazz: {}", clazz);
        clazzService.save(clazz);
        return Result.success();
    }

    // 根据ID查询班级
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据班级查询的ID: {}", id);
        Clazz clazz = clazzService.getInfo(id);
        return Result.success(clazz);
    }

    // 修改班级
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级信息, {}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }



}
